package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.pojo.Color;
import com.luchao.back.service.ColorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "颜色管理")
@RestController
@RequestMapping("/color")
public class ColorController {

    @Autowired
    private ColorService colorService;

    @ApiOperation("分页获取颜色信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getColorByPage")
    public ResponseResult getColorByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = colorService.getColorCount();
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<Color> pageResult = new PageResult<Color>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(colorService.getColorByPage(pageNum, pageSize));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("添加颜色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "color", value = "颜色信息", required = true, paramType = "formData", dataType = "Color", dataTypeClass = Color.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addColor")
    public ResponseResult addColor(@RequestBody Color color) {
        return colorService.addColor(color) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("修改颜色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "color", value = "颜色信息", required = true, paramType = "formData", dataType = "Color", dataTypeClass = Color.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/editColor")
    public ResponseResult editColor(@RequestBody Color color) {
        return colorService.editColor(color) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "修改成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "修改失败", Execute.ERROR);
    }

    @ApiOperation("校验颜色唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "cname", value = "颜色信息", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkColor")
    public ResponseResult checkColor(String cname) {
        return colorService.checkColor(cname) > 0 ? ResponseResult.success(ResponseResult.FAILED, "颜色已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "颜色不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("批量禁用/解禁颜色")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "list", value = "颜色信息", required = true, paramType = "formData", dataType = "List<Color>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchChangeState")
    public ResponseResult batchChangeState(@RequestBody List<Color> list) {
        return colorService.batchChangeState(list) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "操作失败", Execute.ERROR);
    }

    @ApiOperation("获取所有颜色信息")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllColor")
    public ResponseResult getAllColor() {
        return ResponseResult.success(colorService.getAllColor());
    }

    @ApiOperation("获取所有前台展示的颜色信息")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllColorForView")
    public ResponseResult getAllColorForView() {
        return ResponseResult.success(colorService.getAllColorForView());
    }
}
